package cn.xiaodi.site.jdbc.service.impl;

import cn.xiaodi.site.jdbc.mapper.RowMapper;
import cn.xiaodi.site.jdbc.mapper.impl.StudentRowMapper;
import cn.xiaodi.site.jdbc.model.Student;
import cn.xiaodi.site.jdbc.service.IStudentService;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;


/**
 * @author xiao
 * @email 245938767@qq.com
 * @gitee https://gitee.com/liu_yong_qiee/
 * @date 2021/10/20 9:21 上午
 */
@Service
public class StudentServiceImpl implements IStudentService {

    private DataSourceTransactionManager transactionManager;

    private StudentRowMapper studentRowMapper;

    @Override
    public int deleteUser(Integer[] userId) {
        TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition());
        try {
            for (Integer integer : userId) {
                int delete = studentRowMapper.delete(integer);
                if (delete==0){
                    System.out.println("要删除的数据不存在。");
                    transactionManager.rollback(status);
                    return 0;
                }
            }
            transactionManager.commit(status);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            transactionManager.rollback(status);
            return 0;
        }

    }

    public void setTransactionManager(DataSourceTransactionManager transactionManager) {
        this.transactionManager = transactionManager;
    }

    public void setStudentRowMapper(StudentRowMapper studentRowMapper) {
        this.studentRowMapper = studentRowMapper;
    }
}
